package com.pap.base.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.pap.base.util.regex.RegexRule;
import com.pap.base.util.regex.RegexUtilss;
import org.springframework.util.StringUtils;

/**
 * 自定义一个参数校验器，用来判断当前参数是否为中文。
 * @Auther: alexgaoyh
 * @Date: 2018/11/28 17:15
 * @Description:
 */
public class IsPapNameValidator implements ConstraintValidator<IsPapName,String> {

    private boolean required = false;

    //初始化方法
    @Override
    public void initialize(IsPapName constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    //校验方法
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (required == false) {
            return RegexUtilss.isChinese(value);
        }else {
            if (StringUtils.isEmpty(value)) {
                return false;
            }
            return RegexUtilss.isChinese(value);
        }
    }

}
